package day12;


//volatile禁止重排
public class Demo3 {
    static volatile int a,b,c,d;
    public static void main(String[] args) throws InterruptedException {
        int i = 0;//计数
        while (true){
            i++;
            a=b=c=d=0;//每循环一次 数据清0;
            Thread t1 = new Thread(()->{
                a=1;
                c=b;
            });
            Thread t2 = new Thread(()->{
                b=1;
                d=a;
            });
            t1.start();
            t2.start();

            t1.join();
            t2.join();
            if (c==0&&d==0){//重排序了
                System.out.println("执行第"+i+"次出现了重排序");
                break;
            }else {
                System.out.println(i);
            }
        }
    }
}
